package com.gitee.wsl.platform.thread.task.bean

/**
 * For signaling to finalizers how a task exited.
 *
 *
 * Similar to [Outcome], but without the result. Used by [Resource]
 * to indicate how a resource was released.
 */
sealed interface ExitCase {
    /**
     * Signals that the task completed. Used for successful completion, but
     * also for cases in which the outcome is unknown.
     * <br></br>
     * This is a catch-all result. For example, this is the value passed
     * to [Resource.Acquired] in `releaseTask` when the resource
     * is used as an [AutoCloseable], as the `AutoCloseable` protocol
     * does not distinguish between successful completion, failure, or cancellation.
     * <br></br>
     * When receiving a `Completed` exit case, the caller shouldn't
     * assume that the task was successful.
     */
    class Completed : ExitCase {
        companion object {
            val INSTANCE = Completed()
        }
    }

    /**
     * Signals that the task failed with a known exception.
     * <br></br>
     * Used in [Resource.Acquired] to indicate that the resource
     * is being released due to an exception.
     *
     * @param error is the exception thrown
     */
    @JvmRecord
    data class Failed(val error: Throwable?) : ExitCase

    /**
     * Signals that the task was cancelled.
     * <br></br>
     * Used in [Resource.Acquired] to indicate that the resource
     * is being released due to a cancellation (e.g., the thread was
     * interrupted).
     */
    class Canceled : ExitCase {
        companion object {
            val INSTANCE = Canceled()
        }
    }

    companion object {
        fun succeeded(): ExitCase {
            return Completed.INSTANCE
        }

        fun failed(error: Throwable?): ExitCase {
            return Failed(error)
        }

        fun canceled(): ExitCase {
            return Canceled.INSTANCE
        }
    }
}